package com.woyou.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit.RetrofitError;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.citaq.ideliver.R;
import com.woyou.Constant;
import com.woyou.bean.CodeResult;
import com.woyou.bean.Goods;
import com.woyou.bean.GoodsType;
import com.woyou.bean.Info;
import com.woyou.bean.Result;
import com.woyou.bean.ShopDetail;
import com.woyou.bean.ShopItem;
import com.woyou.bean.rpc.GoodsListReq;
import com.woyou.bean.rpc.ShopCollectReq;
import com.woyou.bean.rpc.ShopDetailReq;
import com.woyou.service.LocationLoopService;
import com.woyou.ui.activity.orderdetail.OrderDetailActivity;
import com.woyou.ui.adapter.SuperViewPagerAdapter;
import com.woyou.ui.api.IKeyEventStrategy;
import com.woyou.ui.component.DishesTypePop;
import com.woyou.ui.component.DishesTypePop.ListItemClick;
import com.woyou.ui.component.ErrorHintView;
import com.woyou.ui.component.ErrorHintView.OperateListener;
import com.woyou.ui.component.SuperUI;
import com.woyou.ui.fragment.IndexFragment_;
import com.woyou.ui.fragment.ShopInfoCommentLayout;
import com.woyou.ui.fragment.ShopInfoDetailLayout;
import com.woyou.ui.fragment.ShopInfoMenuLayout;
import com.woyou.ui.fragment.ShopsFragment_;
import com.woyou.utils.CharCheckUtil;
import com.woyou.utils.eventbus.EventClearShoppingCar;
import com.woyou.utils.eventbus.EventGoodsDetails;
import com.woyou.utils.eventbus.EventRestConfirm;

import de.greenrobot.event.EventBus;

/**
 * 店铺信息
 * 
 * @author shenzhou 2014年12月24日
 */
public class ShopInfoActivity extends BaseActivity implements IKeyEventStrategy {
	
	public static final String TAG = "ShopInfoActivity";
	
	private TextView headTitle;
	private ViewPager mViewPager;
	private LinearLayout headView;
	private View bgShade;

	/** 主界面 **/
	private SuperViewPagerAdapter pagerAdapter;
	/** 菜品列表 **/
	ShopInfoMenuLayout menuListView;
	/** 店铺详情 **/
	ShopInfoDetailLayout shopDetailView;
	/** 评论列表 **/
	ShopInfoCommentLayout commentView;
	private ShopDetail shopDetail; // 店铺详情数据
	private List<GoodsType> goodsTypeList; // 菜品类型
	public GoodsType goodsType;//菜品类别

	// 导航条控件
	private View dishes;
	private View info;
	private View comments;
	private ImageView dishesImage; // 菜品旁的箭头
	private TextView dishesType;
	private TextView infoTv;
	private TextView commentsTv;
	private ImageView dishesIv; // 下面的角标
	private ImageView infoIv;
	private ImageView commentsIv;
	private RelativeLayout collection, goodssearchrelayout;
	private ImageView collection_iv, goodssearch;
	private int isCollection = 0;
	
	//店铺商品搜索
	private LinearLayout shopinfodetail, goodskeysearchbtn;
	private RelativeLayout goodssearchview;
	private EditText goodskeydetail;
	private LinearLayout clearkey;
	
	private ShopItem shopItem;	
	private Result<List<Goods>> goodsList;
	
	private DishesTypePop dishesPop; // 菜品的popwindow
	/** 是否弹出菜品的pop标志位 **/
	boolean popFlag = false;
	/** 是否点击2次店铺详情 **/
	boolean infoFlag = true;
	/** 是否点击2次评论 **/
	boolean commentFlag = true;
	private double lat;
	private double lng;
	private String sId; //店铺编号
	private String oriClassName; //从哪个界面传过来
	private GoodsListReq goodsListReq;
	
	private ErrorHintView hintView; // 加载错误提示
	private LocationLoopService loopService;
	
	/**显示店铺列表*/
	private int VIEW_SHOPLIST = 1;
	/**显示断网*/
	private int VIEW_WIFIFAILUER = 2;
	/**显示加载数据失败*/
	private int VIEW_LOADFAILURE = 3;
	/**显示加载中*/
	private int VIEW_LOADING = 4;
	/**显示请求超时*/
	private int VIEW_OUTTIME = 5;
	/**为查询到数据*/
	private int VIEW_UNDATE = 6;
	
	public interface FragmentControllerListener{
		void setPopFlag(boolean flag);
		boolean getPopFlag();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fm_shopinfo);
		initView();
		initEvent();
	}
	
	private void initView() {
		loopService = LocationLoopService.getInstance(mContext);
				
		headTitle = (TextView) findViewById(R.id.head_title);
		mViewPager = (ViewPager) findViewById(R.id.shopinfo_vp);
		headView = (LinearLayout) findViewById(R.id.shopinfo_dish_head);
		bgShade = findViewById(R.id.shade);
		
		hintView = (ErrorHintView)findViewById(R.id.hintView);
		collection = (RelativeLayout)findViewById(R.id.collection);
		collection_iv = (ImageView)findViewById(R.id.collection_iv);
		goodssearchrelayout = (RelativeLayout)findViewById(R.id.goodssearchrelayout);
		goodssearch = (ImageView)findViewById(R.id.goodssearch);
		
		dishes = (View) headView.findViewById(R.id.shopinfo_dishes);
		info = (View) headView.findViewById(R.id.shopinfo_infos);
		comments = (View) headView.findViewById(R.id.shopinfo_comment);
		dishesImage = (ImageView) headView.findViewById(R.id.shopinfo_dishes_image);
		dishesType = (TextView) headView.findViewById(R.id.dishes_type);
		dishesIv = (ImageView) headView.findViewById(R.id.shopinfo_dishes_iv);
		infoIv = (ImageView) headView.findViewById(R.id.shopinfo_info_iv);
		commentsIv = (ImageView) headView.findViewById(R.id.shopinfo_comment_iv);
		infoTv = (TextView) headView.findViewById(R.id.shopinfo_info_tv);
		commentsTv = (TextView) headView.findViewById(R.id.shopinfo_comment_tv);
		shopinfodetail = (LinearLayout) headView.findViewById(R.id.shopinfodetail);
		
		//---------------商品搜索-----------------------------
		goodskeysearchbtn = (LinearLayout) headView.findViewById(R.id.goodskeysearchbtn);
		goodssearchview = (RelativeLayout) headView.findViewById(R.id.goodssearchview);
		goodskeydetail = (EditText) headView.findViewById(R.id.goodskeydetail);
		clearkey = (LinearLayout) headView.findViewById(R.id.clearkey);
		
		goodssearchrelayout.setVisibility(View.VISIBLE);
		collection.setVisibility(View.GONE);
		
		// 初始化数据
		ArrayList<View> views = new ArrayList<View>();
		menuListView = new ShopInfoMenuLayout(getContext());
		menuListView.setFragmentControl(mFragmentControl);
		shopDetailView = new ShopInfoDetailLayout(getContext());
		commentView = new ShopInfoCommentLayout(getContext());
		views.add(menuListView);
		views.add(shopDetailView);
		views.add(commentView);
		pagerAdapter = new SuperViewPagerAdapter(views);
		mViewPager.setAdapter(pagerAdapter);
	}
	
	private void initEvent(){
		findViewById(R.id.back).setOnClickListener(this);
		headTitle.setOnClickListener(this);
		goodssearchrelayout.setOnClickListener(this);
		collection.setOnClickListener(this);
		
		goodskeydetail.addTextChangedListener(new keyEditChangedListener());
		dishes.setOnClickListener(this);
		info.setOnClickListener(this);
		comments.setOnClickListener(this);
		
		goodskeysearchbtn.setOnClickListener(this);
		clearkey.setOnClickListener(this);
		
		// 初始化popwindow
		dishesPop = DishesTypePop.getInstance(getContext(), dishes, bgShade, dishesIv, new FragmentControllerListener() {
			@Override
			public void setPopFlag(boolean flag) {
				popFlag = flag;
			}
			
			@Override
			public boolean getPopFlag() {
				return popFlag;
			}
		});
		
		dishesPop.setmListItemClick(new ListItemClick() {
			@Override
			public void ItemClick(GoodsType goodsType) {
				menuListView.clear();
				ShopInfoActivity.this.goodsType=goodsType;
				menuListView.setSType(goodsType.gettId());
				menuListView.showViewById(VIEW_LOADING);
				menuListView.refreshData(true);
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		EventBus.getDefault().register(this);
		getData();
		
		if(dishesIv.getVisibility()==View.VISIBLE){
			goodssearchrelayout.setVisibility(View.VISIBLE);
			collection.setVisibility(View.GONE);
		} else {
			goodssearchrelayout.setVisibility(View.GONE);
			collection.setVisibility(View.VISIBLE);
		}
		mFragmentControl.openBtn();
		hideSoftInput(mActivity, goodskeydetail);
		if (mShoppingCarModel.getChosenGoodsList().size() == 0) {
			goodskeydetail.setText("");
			shopinfodetail.setVisibility(View.VISIBLE);
			goodssearchview.setVisibility(View.GONE);
		}
		if(goodssearchview.getVisibility()==View.VISIBLE){
			goodssearch.setImageResource(R.raw.goodssearch2);
		} else {
			goodssearch.setImageResource(R.raw.goodssearch1);
		}
		
		// 初始化导航条
		initHead();
		loadItemInfo();
	}

	@Override
	public void onPause() {
		super.onPause();
		EventBus.getDefault().unregister(this);

		dishesPop.hidePopup();
		hideSoftInput(mActivity, goodskeydetail);
		goodskeydetail.setText("");
		shopinfodetail.setVisibility(View.VISIBLE);
		goodssearchview.setVisibility(View.GONE);
		goodssearch.setImageResource(R.raw.goodssearch1);
	
	}
	
	/**
	 * 加载店铺商品和店铺详情
	 */
	private void loadItemInfo() {
		executeTask(new Runnable() {
			
			@Override
			public void run() {
				showViewById(VIEW_LOADING);
				try {
					if (loopService.getChosenAddr()!=null ) {
						lat = Double.parseDouble(loopService.getChosenAddr().getLat());
						lng =Double.parseDouble(loopService.getChosenAddr().getLng());
					}  else {
						BDLocation bdLocation = mBaiduLocationService.getLastLocation();
						if(bdLocation!=null){
							lat = bdLocation.getLatitude();
							lng = bdLocation.getLongitude();
						}else{
							mBaiduLocationService.startLocation();
							mBaiduLocationService.registerLocationListenner(new BDLocationListener() {
								
								@Override
								public void onReceiveLocation(BDLocation bdLocation) {
									loadItemInfo();
								}
							});
							return;
						}
					}
					
					ShopDetailReq shopDetailReq = new ShopDetailReq();
					String uid = mUserModel.getUserInfo().getuId();
					shopDetailReq.setuId(uid); // 用户Id
					shopDetailReq.setsId(sId);
					shopDetailReq.setLng(lng + ""); // 用户经度
					shopDetailReq.setLat(lat + ""); // 用户纬度
					Result<ShopDetail> result = mShopsModel.v2_3queryShopInfo(shopDetailReq);
					
					if (result!=null && result.getCode() == 1) {
						shopDetail = result.getData();
						if ( !TextUtils.isEmpty(oriClassName) && oriClassName.equals(OrderDetailActivity.class.getName()) ) {
							setUpShopItem(shopDetail);
							
						} else if ( !TextUtils.isEmpty(oriClassName) && oriClassName.equals(ActivitiesActivity_.class.getName()) ) {
							runOnUI(new Runnable() {
								
								@Override
								public void run() {
									headTitle.setText(shopDetail.getsName());
								}
							});
							setUpShopItem(shopDetail);
							
						} else if ( !TextUtils.isEmpty(oriClassName) && oriClassName.equals(IndexFragment_.class.getName()) ) {
							runOnUI(new Runnable() {
								
								@Override
								public void run() {
									headTitle.setText(shopDetail.getsName());
								}
							});
							setUpShopItem(shopDetail);
						}
						
						goodsTypeList = shopDetail.getGoodsTypeList();
						// 设置店铺详情
						mShoppingCarModel.setShopDetail(shopDetail);
						// 获取商品列表
						loadGoodsList(goodskeydetail.getText().toString(),false);
						// 设置店铺详情
						runOnUI(new Runnable() {
							@Override
							public void run() {
								shopDetailView.fillData(shopDetail);
								if (shopDetail.getIsCollection() == 1) {
									collection_iv.setImageResource(R.raw.collection);
									// collection.setVisibility(View.GONE);
								} else {
									collection_iv.setImageResource(R.raw.uncollection);
									// collection.setVisibility(View.GONE);
								}
							}
						});
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						break;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						break;
					case NETWORK:
						showViewById(VIEW_WIFIFAILUER);
						break;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						break;
					}
				}
			}
		});
	}
	
	/**
	 * 重置shopItem
	 * @param detail
	 */
	private void setUpShopItem(final ShopDetail detail){
		runOnUI(new Runnable() {
			
			@Override
			public void run() {
				if ( shopItem==null ) {
					shopItem = new ShopItem();
				}
				if ( !TextUtils.isEmpty(sId) ) {
					shopItem.setsId(sId);
				} else {
					shopItem.setsName(detail.getsId());
				}
				shopItem.setsName(detail.getsName());
				shopItem.setState(detail.getState());
				shopItem.setSpending(detail.getToSendPrice());
				shopItem.setAvgSend(detail.getAvgSend());
			}
		});
	}

	/**
	 * 加载商品列表
	 * @param key 关键字
	 * @param isType 是否检测类别
	 */
	private void loadGoodsList(final String key,final boolean isType) {
		executeTask(new Runnable() {
			
			@Override
			public void run() {
				showViewById(VIEW_LOADING);
				try {
					goodsListReq = new GoodsListReq();
					goodsListReq.setsId(sId);
					goodsListReq.setPage(1);
					goodsListReq.setKey(key);
					if(goodsType!=null&&isType){
						goodsListReq.setsType(""+goodsType.gettId());
					}else{
						goodsListReq.setsType("");
					}
					
					goodsList = mShopsModel.v2_2queryGoodsList(goodsListReq);
					if (goodsList != null && goodsList.getCode() == 1) {
						if(goodsList.getData() != null)
						{
							runOnUI(new Runnable() {
								@Override
								public void run() {
									showViewById(VIEW_SHOPLIST);
									menuListView.initView(shopItem, shopDetail, goodsList);
								}
							});
						}
						else
						{
							showViewById(VIEW_UNDATE);
						}
					} else {
						showViewById(VIEW_LOADFAILURE);
					}
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showViewById(VIEW_OUTTIME);
						break;
					case CONVERSION:
						showViewById(VIEW_LOADFAILURE);
						break;
					case NETWORK:
						showViewById(VIEW_OUTTIME);
						break;
					case UNEXPECTED:
						showViewById(VIEW_LOADFAILURE);
						break;
					}
					
				} 
			}
		});
	}

	/**初始化导航条*/
	private void initHead() {
		if (shopItem != null) {
			if (!TextUtils.isEmpty(shopItem.getsName())) {
				headTitle.setText(shopItem.getsName());
			}
			// if ( !TextUtils.isEmpty(shopItem.commentsNum) ){
			// commentsTv.setText("评论  (" + shopItem.commentsNum + ")");
			// } else {
			// commentsTv.setText("评论");
			// }
		}
		if(goodsTypeList!=null){
			dishesType.setText("全部商品");
			dishesPop.clear();
			dishesPop.init(goodsTypeList);
		}
	}
	
	/**返回上一级菜单*/
	private void back() {
		hideSoftInput(mActivity, goodskeydetail);
		if( isClose )
			return;

		if(dishesImage.getVisibility()==View.VISIBLE) {
			if (menuListView.isShoppingCarShow()) {
				menuListView.hideShoppingCar();
			} else if (menuListView.isPropPanelShow()) {
				menuListView.hidePropPanel();
			} else {
				if (mShoppingCarModel.getChosenGoodsList().size() > 0) {
					SuperUI.openBottomDialog(mContext, "确认要清空购物篮吗？", "",
							new OnClickListener() {

								@Override
								public void onClick(View arg0) {
									SuperUI.dismiss();
								}
							}, "", new OnClickListener() {
								@Override
								public void onClick(View arg0) {
									showViewById(VIEW_LOADING);
									backClearShopCar();
									menuListView.clear();
									SuperUI.dismiss();
								}
							});
				} else if(goodssearchview.getVisibility()==View.VISIBLE){
					// 店铺搜索
					hideSoftInput(mActivity, goodskeydetail);
					goodskeydetail.setText("");
					loadGoodsList(goodskeydetail.getText().toString(),false);
					shopinfodetail.setVisibility(View.VISIBLE);
					goodssearchview.setVisibility(View.GONE);
					goodssearch.setImageResource(R.raw.goodssearch1);
					// 重置过滤器
					TextView text = (TextView) dishes.findViewById(R.id.dishes_type);
					text.setText("全部商品");
					
				}else {
					showViewById(VIEW_LOADING);
					menuListView.clear();
					backClearShopCar();
				}
			}
		}
		else {
			shopInfoView();
		}
	
	}

	/**
	 * 退出清除购物车
	 */
	@SuppressWarnings("rawtypes")
	public void backClearShopCar() {
		if (menuListView.isPropPanelShow()) {
			menuListView.hidePropPanel();
		} else {
			menuListView.clear();
			// 清空购物车
			EventBus.getDefault().post(new EventClearShoppingCar());
			//重置确认订单页面
			EventBus.getDefault().post(new EventRestConfirm());
			// 重置导航布局
			reset();
			mViewPager.setCurrentItem(0);
			// 清空购物车
			// mShoppingCarModel.clearShopppingCar();
			
			// 跳转到店铺列表界面
			Info<Class> info = new Info<Class>();
			info.setData(ShopsFragment_.class);
			openActivity(HomeActivity_.class, info);
			finish();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
		case R.id.head_title:
			// 返回按钮
			back();
			break;
		
		case R.id.item_menulist_submit:
			break;
		case R.id.shopinfo_dishes:
			hintView.setVisibility(View.GONE);
			goodssearchrelayout.setVisibility(View.VISIBLE);
			collection.setVisibility(View.GONE);
			shopInfoView();
			break;
			
		case R.id.shopinfo_infos:
			hintView.setVisibility(View.GONE);
			dishesType.setTextColor(getResources().getColor(R.color.light_grey));
			infoTv.setTextColor(getResources().getColor(R.color.black));
			commentsTv.setTextColor(getResources().getColor(R.color.light_grey));
			goodssearchrelayout.setVisibility(View.GONE);
			collection.setVisibility(View.VISIBLE);
			if (shopDetail != null) {
				if (infoFlag) {
					shopDetailView.fillData(shopDetail);
					infoFlag = false;
				}
				mViewPager.setCurrentItem(1);
				dishesImage.setVisibility(View.INVISIBLE);
				dishesIv.setVisibility(View.INVISIBLE);
				infoIv.setVisibility(View.VISIBLE);
				commentsIv.setVisibility(View.INVISIBLE);
				popFlag = true;
			}
			break;
			
		case R.id.shopinfo_comment:
			hintView.setVisibility(View.GONE);
			dishesType.setTextColor(getResources().getColor(R.color.light_grey));
			infoTv.setTextColor(getResources().getColor(R.color.light_grey));
			commentsTv.setTextColor(getResources().getColor(R.color.black));
			goodssearchrelayout.setVisibility(View.GONE);
			collection.setVisibility(View.VISIBLE);
			
			if (commentFlag) {
				commentView.initView(shopItem);
				// 第一次加载的是全部评论
				commentView.loadAllCommentsData(true);
				commentFlag = false;
			}
			mViewPager.setCurrentItem(2);
			dishesImage.setVisibility(View.INVISIBLE);
			dishesIv.setVisibility(View.INVISIBLE);
			infoIv.setVisibility(View.INVISIBLE);
			commentsIv.setVisibility(View.VISIBLE);
			popFlag = true;
			break;
			
		case R.id.collection:
			// 收藏
			if (mUserModel.getUserInfo().getuId() != null && !mUserModel.getUserInfo().getuId().equals("")) {
				collectionShopReq();
			} else {
				openActivity(LoginActivity_.class, null);
			}

			break;
		case R.id.goodssearchrelayout:
			//显示商品搜索框
			if(goodssearchview.getVisibility()==View.GONE){
				menuListView.hidePropPanel();
				shopinfodetail.setVisibility(View.GONE);
				goodssearchview.setVisibility(View.VISIBLE);
				goodssearch.setImageResource(R.raw.goodssearch2);
			} else {
				hideSoftInput(mActivity, goodskeydetail);
				goodskeydetail.setText("");
				loadGoodsList(goodskeydetail.getText().toString(),true);
				shopinfodetail.setVisibility(View.VISIBLE);
				goodssearchview.setVisibility(View.GONE);
				goodssearch.setImageResource(R.raw.goodssearch1);
			}
			break;
		case R.id.goodskeysearchbtn:
			//搜索店铺商品
			if ( !TextUtils.isEmpty(goodskeydetail.getText().toString()) && !CharCheckUtil.isEmpty(goodskeydetail.getText().toString()) ) {
				hideSoftInput(mActivity, goodskeydetail);
				loadGoodsList(goodskeydetail.getText().toString(),false);
			} else {
				showToast("湿主，洒家还不知道您要搜什么?");
			}
			break;
		case R.id.clearkey:
			//清空搜索关键字
			goodskeydetail.setText("");
			clearkey.setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * 显示菜品列表
	 */
	public void shopInfoView() {
		collection.setVisibility(View.GONE);
		goodssearchrelayout.setVisibility(View.VISIBLE);
		dishesType.setTextColor(getResources().getColor(R.color.black));
		infoTv.setTextColor(getResources().getColor(R.color.light_grey));
		commentsTv.setTextColor(getResources().getColor(R.color.light_grey));
		menuListView.hideShoppingCar();
		menuListView.hidePropPanel();
		if ( popFlag ) {
			mViewPager.setCurrentItem(0);
			menuListView.listAdapter.notifyDataSetChanged();
			dishesImage.setVisibility(View.VISIBLE);
			dishesIv.setVisibility(View.VISIBLE);
			infoIv.setVisibility(View.INVISIBLE);
			commentsIv.setVisibility(View.INVISIBLE);
			dishesPop.hidePopup();
			popFlag = false;
		} else {
			if (goodsTypeList != null && goodsTypeList.size() > 0) {
				dishesPop.init(goodsTypeList);
				dishesPop.showPopup();
				dishesIv.setVisibility(View.GONE);
			} else {
				dishesImage.setVisibility(View.INVISIBLE);
			}
			popFlag = true;
		}
	}
	
	/**
	 * 收藏店铺
	 */
	public void collectionShopReq() {
		executeTask(new Runnable() {
			@SuppressWarnings("rawtypes")
			@Override
			public void run() {
				try {
					if (shopDetail.getIsCollection() == 1) {
						isCollection = 2;
					} else {
						isCollection = 1;
					}
					ShopCollectReq shopCollectReq = new ShopCollectReq();
					shopCollectReq.setuId(mUserModel.getUserInfo().getuId());
					shopCollectReq.setPwd(mUserModel.getUserInfo().getPwd());
					String[] sId = new String[1];
					sId[0] = shopDetail.getsId();
					shopCollectReq.setsIdList(sId);
					shopCollectReq.setIscollect(isCollection);
					final CodeResult result = mShopsModel.shopCollect(shopCollectReq);
					mActivity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (result!=null&&result.getCode() == 1) {
								if (isCollection == 1) {
									shopDetail.setIsCollection(1);
									collection_iv.setImageResource(R.raw.collection);
									SuperUI.showCollectionUI(getContext());
								} else {
									shopDetail.setIsCollection(2);
									collection_iv.setImageResource(R.raw.uncollection);
									SuperUI.showUncollectionUI(getContext());
								}
							}else if (result != null&& -3 == result.code) {
								showToast(result.getMsg());
								openActivity(LoginActivity_.class, null);
							} else {
								showToast("很抱歉,服务器又任性了");
							}

						}
					});
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showToastOnUI("很遗憾,网络不给力");
						return;
					case CONVERSION:
						showToastOnUI("很抱歉,服务器又任性了");
						return;
					case NETWORK:
						showToastOnUI("很遗憾,网络不给力");
						return;
					case UNEXPECTED:
						showToastOnUI("很抱歉,服务器又任性了");
						return;
					}
				}
			}
		});
	}

	/**
	 * 重置导航布局
	 */
	private void reset() {
		dishesImage.setVisibility(View.VISIBLE);
		dishesIv.setVisibility(View.VISIBLE);
		infoIv.setVisibility(View.INVISIBLE);
		commentsIv.setVisibility(View.INVISIBLE);
		
		headTitle.setText("");
		
		popFlag = false;
		menuListView.clearData();
		if( goodsTypeList != null )
		    goodsTypeList.clear();
		
		goodsType=null;
		dishesType.setText("全部商品");
		dishesPop.clear();
		
		dishesType.setTextColor(getResources().getColor(R.color.light_grey));
		infoTv.setTextColor(getResources().getColor(R.color.light_black));
		commentsTv.setTextColor(getResources().getColor(R.color.light_black));

		commentFlag = true;
		commentView.clearCommentsList();

		infoFlag = true;
		shopDetailView.clear();

		System.gc();
	}
	
	/**
	 * 显示商品大图
	 * @param event
	 */
	public void onEvent(EventGoodsDetails event) {
		Info<String> info = new Info<String>(event.getUrl());
		info.setOrigin(ShopInfoActivity.class);
		openActivity(GoodsDetailsActivity_.class, info);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void getData() {  
		if(hasInfo()){
			Object obj = mInfo.getData();
			Class originClzz = mInfo.getOriginClzz();
			if(originClzz!=null){
				oriClassName = originClzz.getName();
			}
			
			if (obj instanceof ShopItem) {
				// 店铺列表、店铺搜索、我的优惠券、我的收藏
				shopItem = (ShopItem) obj;
				sId = shopItem.getsId();
				
			} else if(obj instanceof String){
				// 广告页面或者首页
				sId = (String) obj;
				
			} else if(obj instanceof Map){
				// 订单详情
				Map<String, String> map = (Map<String, String>) obj;
				sId = map.get("sId");
				headTitle.setText(map.get("sName"));
			}
			
		}
	}

	@Override
	public boolean backOperate() {
		if(dishesImage.getVisibility()==View.VISIBLE) {
			if (menuListView.isShoppingCarShow()) {
				menuListView.hideShoppingCar();
			} else if (menuListView.isPropPanelShow()) {
				menuListView.hidePropPanel();
			} else {
				if (mShoppingCarModel.getChosenGoodsList().size() > 0) {
					SuperUI.openBottomDialog(mContext, "确认要清空购物篮吗？", "",
							new OnClickListener() {

								@Override
								public void onClick(View arg0) {
									SuperUI.dismiss();

								}
							}, "", new OnClickListener() {

								@Override
								public void onClick(View arg0) {
									showViewById(VIEW_LOADING);
									backClearShopCar();
									menuListView.clear();
									SuperUI.dismiss();
								}
							});
				} else if(goodssearchview.getVisibility()==View.VISIBLE){
					hideSoftInput(mActivity, goodskeydetail);
					goodskeydetail.setText("");
					loadGoodsList(goodskeydetail.getText().toString(),false);
					shopinfodetail.setVisibility(View.VISIBLE);
					goodssearchview.setVisibility(View.GONE);
					goodssearch.setImageResource(R.raw.goodssearch1);
				}else {
					showViewById(VIEW_LOADING);
					menuListView.clear();
					backClearShopCar();
				}
			}
			return true;
		}
		else {
			shopInfoView();
		}
		return true;
	}

	/**
	 * 1显示列表：,2: 显示断网：3 显示加载数据失败：4 显示加载中：5 显示请求超时:
	 */
	public void showViewById(final int i) {
		runOnUI(new Runnable() {
			
			@Override
			public void run() {
				hintView.setVisibility(View.VISIBLE);
				switch (i) {
				case 1:
					hintView.close();
					break;
				case 2:
					hintView.netError(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							loadItemInfo();
						}
					});
					break;
				case 3:
					hintView.loadFailure(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							loadItemInfo();
						}
					});
					break;
				case 4:
					hintView.loadingData();
					break;
				case 5:
					hintView.timeOut(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							loadItemInfo();
						}
					});
					break;
				case 6:
					hintView.noData(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							loadItemInfo();
						}
					});
					break;
				}
			}
		});
	}
	
	
	/**
	 * 店铺搜索关键字监听
	 * 
	 * @author tlc 2015年1月9日
	 */
	class keyEditChangedListener implements TextWatcher {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}
		@Override
		public void afterTextChanged(Editable s) {
			if (goodskeydetail.getText().toString().length() > 0) {
				Constant.shoppingkey = goodskeydetail.getText().toString();
				clearkey.setVisibility(View.VISIBLE);
			} else {
				Constant.shoppingkey = "";
				clearkey.setVisibility(View.GONE);
			}
		}
	}
	
	@Override
	public boolean eventOperate(KeyEvent event) {
		//拦截确定按钮事件
		if( event.getKeyCode() == KeyEvent.KEYCODE_ENTER ){
			String searchKey = goodskeydetail.getText().toString();
			if ( !TextUtils.isEmpty(searchKey) && !CharCheckUtil.isEmpty(searchKey) ) {
				hideSoftInput(mActivity, goodskeydetail);
				loadGoodsList(goodskeydetail.getText().toString(),false);
			} else {
				showToast("湿主，洒家还不知道您要搜什么?");
			}
			return true;
		}
		return false;
	}
	
	private boolean isClose = false;
	FragmentControl mFragmentControl = new FragmentControl() {
		
		@Override
		public void closeBtn() {
			isClose = true;
			
		}

		@Override
		public void openBtn() {
			isClose = false;
		}
	};
	
	public interface FragmentControl{
		void closeBtn();
		void openBtn();
	}
	
	
}
